import XLSX from 'xlsx'

const { utils } = XLSX

const defColorRule = [
    {
        rule: [0, 5],
        color: '#1552d1'
    },
    {
        rule: [5, 10],
        color: '#4fd50a'
    },
    {
        rule: [10, 15],
        color: '#feff00'
    },
    {
        rule: [15, 25],
        color: '#ff2321'
    },
    {
        rule: [25, 10000000],
        color: '#5e1300'
    }
]
const ruleTextarea = document.querySelector('#RuleTextarea')
const isJson = function (val) {
    try {
        return JSON.parse(val)
    } catch (e) {
        return false
    }
}
const ruleStore = {
    name: "COLOR_RULE",
    get() {
        let d = localStorage.getItem(name)
        if (isJson(d)) {
            d = JSON.parse(d)
        } else {
            d = ''
        }
        return d || defColorRule
    },
    set(val = ruleTextarea.value) {
        val = val.replace(/\s/ig, '')
        console.log(val)
        if (isJson(val)) {
            localStorage.setItem(name, val)
            return true
        } else {
            alert('格式错误')
            return false
        }
    }
}
export function number2color(num) {
    num = Number(num)
    let arr = ruleStore.get()
    let color = '#ff4400'
    for (let i of arr) {
        if (i.rule.length > 1) {
            if (num > i.rule[0] && num <= i.rule[1]) {
                color = i.color
            }
        } else {
            if (num === i.rule[0]) {
                color = i.color
            }
        }
    }
    return color
}

export function readFile(file) {
    return new Promise((resolve, reject) => {
        let reader = new FileReader()
        reader.onload = e => {
            let data = e.target.result
            try {
                let json = XLSX.read(data, { type: 'binary' })
                const sheets = json.Sheets['Sheet1']
                let list = utils.sheet_to_json(sheets, { header: 'A' })
                list.splice(0, 1)
                resolve(list)
            } catch (e) {
                reject(e)
            }
        }
        reader.readAsBinaryString(file)
    })
}

// 线宽操作
export function strokeWidthSetting() {
    const strokeWidth = document.querySelector('#StrokeWidth')
    strokeWidth.value = localStorage.getItem('STROKE_WIDTH') || 10
    strokeWidth.addEventListener('change', e => {
        localStorage.setItem('STROKE_WIDTH', strokeWidth.value)
    })
    return strokeWidth
}

// 颜色规则设置
export function ruleModalSetting() {
    let ruleModal = document.querySelector('#RuleModal')
    ruleModal.show = function () {
        this.style.display = 'block'
    }
    ruleModal.hide = function () {
        this.style.display = 'none'
    }

    document.querySelector('#RuleBtn').addEventListener('click', e => {
        ruleModal.show()
        ruleTextarea.value = JSON.stringify(ruleStore.get(), null, 2)
    })

    document.querySelector('#RuleConfirmBtn').addEventListener('click', e => {
        if (ruleStore.set()) {
            ruleModal.hide()
        }
    })
    document.querySelector('#RuleCancelBtn').addEventListener('click', e => {
        console.log
        ruleModal.hide()
    })
}